﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.Reflection;
    using UnityEngine;

    internal class CustomEditorAttributes
    {
        private static ArrayList m_CustomEditors;
        private static ArrayList m_CustomMultiEditors;

        internal static void Clear()
        {
            m_CustomEditors = new ArrayList();
            m_CustomMultiEditors = new ArrayList();
        }

        internal static System.Type FindCustomEditorType(UnityEngine.Object o, bool multiEdit)
        {
            if (m_CustomEditors == null)
            {
                Clear();
                Assembly[] loadedAssemblies = EditorAssemblies.loadedAssemblies;
                for (int i = loadedAssemblies.Length - 1; i >= 0; i--)
                {
                    Rebuild(loadedAssemblies[i]);
                }
            }
            System.Type type = o.GetType();
            IEnumerator enumerator = (!multiEdit ? m_CustomEditors : m_CustomMultiEditors).GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    MonoEditorType current = (MonoEditorType) enumerator.Current;
                    if (current.inspectedType == type)
                    {
                        return current.inspectorType;
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
            return TryLoadLogicGraphInspector(o);
        }

        internal static void Rebuild(Assembly assembly)
        {
            foreach (System.Type type in AssemblyHelper.GetTypesFromAssembly(assembly))
            {
                foreach (CustomEditor editor in type.GetCustomAttributes(typeof(CustomEditor), false))
                {
                    MonoEditorType type2 = new MonoEditorType();
                    if (editor.m_InspectedType == null)
                    {
                        Debug.Log("Can't load custom inspector " + type.Name + " because the inspected type is null.");
                    }
                    else if (!type.IsSubclassOf(typeof(Editor)))
                    {
                        if (((type.FullName != "TweakMode") || !type.IsEnum) || (editor.m_InspectedType.FullName != "BloomAndFlares"))
                        {
                            Debug.LogWarning(type.Name + " uses the CustomEditor attribute but does not inherit from Editor.\nYou must inherit from Editor. See the Editor class script documentation.");
                        }
                    }
                    else
                    {
                        type2.inspectedType = editor.m_InspectedType;
                        type2.inspectorType = type;
                        m_CustomEditors.Add(type2);
                        if (type.GetCustomAttributes(typeof(CanEditMultipleObjects), false).Length > 0)
                        {
                            m_CustomMultiEditors.Add(type2);
                        }
                    }
                }
            }
        }

        internal static System.Type TryLoadLogicGraphInspector(UnityEngine.Object o)
        {
            return null;
        }

        private class MonoEditorType
        {
            public System.Type inspectedType;
            public System.Type inspectorType;
        }
    }
}

